package com.itpractice.mvpdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.login.presenter.ILoginPresenter;
import com.login.presenter.LoginPresenterCompl;
import com.login.view.ILoginView;

public class LoginMainActivity extends Activity implements ILoginView,
		OnClickListener {
	private EditText editUser;
	private EditText editPass;
	private Button btnLogin;
	private Button btnClear;
	private ProgressBar progressBar;

	private ILoginPresenter loginPresenter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// find view
		editUser = (EditText) this.findViewById(R.id.et_login_username);
		editPass = (EditText) this.findViewById(R.id.et_login_password);
		btnLogin = (Button) this.findViewById(R.id.btn_login_login);
		btnClear = (Button) this.findViewById(R.id.btn_login_clear);
		progressBar = (ProgressBar) this.findViewById(R.id.progress_login);

		// set Listener
		btnClear.setOnClickListener(this);
		btnLogin.setOnClickListener(this);

		// init
		loginPresenter = new LoginPresenterCompl(this);
		loginPresenter.setProgressBarVisibility(View.INVISIBLE);
	}

	@Override
	public void onClearText() {
		editPass.setText("");
		editUser.setText("");
	}

	@Override
	public void onLoginResult(Boolean result, int code) {
		loginPresenter.setProgressBarVisibility(View.INVISIBLE);
		btnLogin.setEnabled(true);
		btnClear.setEnabled(true);
		if(result){
			Toast.makeText(this, "Login Success", 1).show();
		}else{
			Toast.makeText(this, "Login Fail, code = ", 1).show();
		}
	}

	@Override
	public void onSetProgressBarVisibility(int visibility) {
		progressBar.setVisibility(visibility);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login_clear:
			loginPresenter.clear();
			break;
		case R.id.btn_login_login:
			loginPresenter.setProgressBarVisibility(View.VISIBLE);
			btnLogin.setEnabled(false);
			btnClear.setEnabled(false);
			loginPresenter.doLogin(editUser.getText().toString(), editPass.getText().toString());
			break;
		}
	}
}
